#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <map>

using namespace std;

// 3440. 重新安排会议得到最多空余时间 II
// https://leetcode.cn/problems/reschedule-meetings-for-maximum-free-time-ii/description/?envType=daily-question&envId=2025-07-10

class Solution
{
public:
    int maxFreeTime(int eventTime, vector<int> &startTime, vector<int> &endTime)
    {
        int n = startTime.size();
        auto get = [&](int i) -> int
        {
            if (i < 0)
            {
                return 0;
            }
            if (i == 0)
            {
                return startTime[0];
            }
            if (i == startTime.size())
            {
                return eventTime - endTime[n - 1];
            }
            return startTime[i] - endTime[i - 1];
        };

        int a = 0;
        int b = -1;
        int c = -1;
        for (int i = 1; i <= n; i++)
        {
            int sz = get(i);
            if (sz > get(a))
            {
                c = b;
                b = a;
                a = i;
            }
            else if (b == -1 || sz > get(b))
            {
                c = b;
                b = i;
            }
            else if (c == -1 || sz > get(c))
            {
                c = i;
            }
        }

        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            int sz = endTime[i] - startTime[i];
            if (i != a && i + 1 != a && sz <= get(a) || i != b && i + 1 != b && sz <= get(b) || sz <= get(c))
            {
                ans = max(sz + get(i + 1) + get(i), ans);
            }
            else
            {
                ans = max(ans, get(i + 1) + get(i));
            }
        }
        return ans;
    }
};

int main()
{
    return 0;
}